package com.gikkiares.gafoundationdemo.home;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;


import com.gikkiares.gafoundationdemo.module.gesture_password_view.GesturePasswordActivity;
import com.vgemv.jsutilitysdk.activity.BaseActivity.JsBaseActivity;
import com.vgemv.jsutilitysdk.activity.jsaboutactivity.JsAboutActivity;
import com.vgemv.jsutilitysdk.JsUtilitySdkManager;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.List.JsListItemVm;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewAdapter;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewBasicCallback;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewVm;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.List.JsListItemView;
import com.vgemv.jsutilitysdkdemo.Env;
import com.vgemv.jsutilitysdkdemo.module.Application.AppApplication;
import com.vgemv.jsutilitysdkdemo.module.ChooseApp.ChooseAppTesterActivity;
import com.vgemv.jsutilitysdkdemo.module.Crash.CrashActivity;
import com.vgemv.jsutilitysdkdemo.module.List.Chat.ChatActivity;
import com.vgemv.jsutilitysdkdemo.module.List.ExtendContent.ExtendContentActivity;
import com.vgemv.jsutilitysdkdemo.module.List.WordLive.WordLiveActivity;
import com.vgemv.jsutilitysdkdemo.module.Menu.FilmList.FilmListActivity;
import com.vgemv.jsutilitysdkdemo.module.Menu.MenuActivity;
import com.vgemv.jsutilitysdkdemo.module.log.LogActivity;
import com.vgemv.jsutilitysdkdemo.module.Download.DownloadActivity;
import com.vgemv.jsutilitysdkdemo.module.Menu.Share.MenuViewActivity;
import com.vgemv.jsutilitysdkdemo.module.NetworkState.NetworkStateActivity;
import com.vgemv.jsutilitysdkdemo.module.Page.PageActivity;
//import com.vgemv.jsutilitysdkdemo.Module.Reminder.ReminderListActivity;
import com.vgemv.jsutilitysdkdemo.module.Request.RequestActivity;
import com.vgemv.jsutilitysdkdemo.module.Screen.ScreenActivity;
import com.vgemv.jsutilitysdkdemo.module.Sensor.SensorActivity;
import com.vgemv.jsutilitysdkdemo.module.StringValidator.StringValidatorActivity;
import com.vgemv.jsutilitysdkdemo.module.TextView.JsTextViewActivity;
import com.vgemv.jsutilitysdkdemo.R;
import com.vgemv.jsutilitysdkdemo.module.preview.VideoPreviewActivity;
import com.vgemv.jsutilitysdkdemo.module.share.ShareActivity;

import java.util.ArrayList;

// AppCompatActivity
public class HomeActivity extends JsBaseActivity {

    LinearLayout mLinearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestAhthority();

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setId(View.generateViewId());
        setContentView(linearLayout);

        JsMenuView menuView = new JsMenuView(this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addContentView(menuView,lp);
        
        menuView.mBasicCallback = new JsMenuViewBasicCallback() {
            @Override
            public ArrayList<JsMenuItemVm> jsMenuViewSetDatasource() {
                ArrayList<JsMenuItemVm> models = new ArrayList<>();
                models.add(new JsListItemVm(0, "GesturePassword", "GesturePassword", GesturePasswordActivity.class));
                models.add(new JsListItemVm(0, "Reminder", "Useful reminder"));
                models.add(new JsListItemVm(1, "Log", "JsLogManager"));
                models.add(new JsListItemVm(2, "Download", "xxxx"));
                models.add(new JsListItemVm(3, "Menu", "各种菜单的显示"));
                models.add(new JsListItemVm(4, "LiveWordList", "标准图文列表的显示"));
                models.add(new JsListItemVm(5, "ChatList", "标准聊天列表的显示"));
                models.add(new JsListItemVm(6, "ExtentContentList", "标准聊天列表的显示"));
                models.add(new JsListItemVm(7, "PageActivity", "标准PageActivity显示"));
                models.add(new JsListItemVm(8, "WindowActivity", "获取Window"));
                models.add(new JsListItemVm(9, "BottomInputBar", "BottomInputBar"));
                models.add(new JsListItemVm(10, "Screen", "屏幕尺寸分析"));
                models.add(new JsListItemVm(11, "Sensor", "传感器"));
                models.add(new JsListItemVm(12, "NetworkStateMonitor", "网络状态监控"));
                models.add(new JsListItemVm(13, "JsMenuView with animation", "菜单的动画显示"));
                models.add(new JsListItemVm(14, "FilmListActivity", "FilmList"));
                models.add(new JsListItemVm(15, "StringValidator", "检测字符串"));
                models.add(new JsListItemVm(16, "TestTextView", "TextView检测"));
                models.add(new JsListItemVm(17, "Crash", "Crash检测"));
                models.add(new JsListItemVm(18, "Request", "网络请求"));
                models.add(new JsListItemVm(19, "ChooseApp", "ChooseAppTester"));
                models.add(new JsListItemVm(20, "Share", "Share file"));
                models.add(new JsListItemVm(21, "VideoPreview", "Share file"));
                return models;
            }

            @Override
            public void jsMenuViewClickItem(JsMenuView menuView, int i, JsMenuItemVm vm) {

                JsListItemVm listItemVm = (JsListItemVm) vm;
                if(listItemVm.cls != null) {
                    Intent intent = new Intent(HomeActivity.this, listItemVm.cls);
                    startActivity(intent);
                    return;
                }
                switch (i) {
                    case 0:{
//                        Intent intent = new Intent(HomeActivity.this, ReminderListActivity.class);
//                        startActivity(intent);

                        break;
                    }
                    case 1:{
                        Intent intent = new Intent(HomeActivity.this, LogActivity.class);
                        startActivity(intent);
                        break;
                    }

                    case 2:{
                        Intent intent = new Intent(HomeActivity.this, DownloadActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 3:{
                        Intent intent = new Intent(HomeActivity.this, MenuActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 4:{
                        Intent intent = new Intent(HomeActivity.this, WordLiveActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 5:{
                        Intent intent = new Intent(HomeActivity.this, ChatActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 6:{
                        Intent intent = new Intent(HomeActivity.this, ExtendContentActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 7:{
                        Intent intent = new Intent(HomeActivity.this, PageActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 10:{
                        Intent intent = new Intent(HomeActivity.this, ScreenActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 12:{
                        Intent intent = new Intent(HomeActivity.this, NetworkStateActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 13: {
                        Intent intent = new Intent(HomeActivity.this, MenuViewActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 14: {
                        Intent intent = new Intent(HomeActivity.this, FilmListActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 15: {
                        Intent intent = new Intent(HomeActivity.this, StringValidatorActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 16: {
                        Intent intent = new Intent(HomeActivity.this, JsTextViewActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 17: {
                        Intent intent = new Intent(HomeActivity.this, CrashActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 18: {
                        Intent intent = new Intent(HomeActivity.this, RequestActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 19: {
                        Intent intent = new Intent(HomeActivity.this, ChooseAppTesterActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 20: {
                        Intent intent = new Intent(HomeActivity.this, ShareActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 21: {
                        Intent intent = new Intent(HomeActivity.this, VideoPreviewActivity.class);
                        startActivity(intent);
                        break;
                    }
                    default:{
                        Intent intent = new Intent(HomeActivity.this, SensorActivity.class);
                        startActivity(intent);
                        break;
                    }
                }


            }


            @Override
            public JsMenuItemView jsMenuViewCreateItemView(JsMenuView menuView, Context context) {
                JsListItemView itemView = new JsListItemView(context);
                return itemView;
            }

            @Override
            public void jsMenuViewAfterCreateView(JsMenuView menuView) {
                menuView.scrollToBottom();
            }

            @Override
            public JsMenuViewVm jsMenuViewSetVm(JsMenuView menuView) {
                JsMenuViewVm vm = new JsMenuViewVm();
                vm.mi_layoutStyle = 2;
                return vm;
            }

            @Override
            public Rect jsMenuViewSetItemOffset(JsMenuView menuView, int index) {

                    Rect rect = new Rect();
                    int space = 10;
                    rect.left = space;
                    rect.right = space;
                    rect.bottom = space;
                    if (index == 0) {
                        rect.top = space;
                    }
                    return rect;
            }

            @Override
            public JsMenuViewAdapter jsMenuViewSetAdapter(JsMenuView menuView) {
                return null;
            }

            @Override
            public void jsMenuItemViewWillDisplay(RecyclerView.ViewHolder viewHolder, int position) {

            }
        };


    }

    public void requestAhthority() {
        String[] permissions =  new String[]{
                Manifest.permission.CAMERA,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.INTERNET,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.MODIFY_AUDIO_SETTINGS,
                Manifest.permission.BROADCAST_STICKY,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.ACCESS_NETWORK_STATE
        };

        IPermissionRequestCallback callback = new IPermissionRequestCallback() {
            @Override
            public void onAuthorityAllGranted() {
                Log.d("TAG","onAuthorityAllGranted");
            }

            /**
             * Not all permission granted, remember add permission at AndroidManifest.xml
             * @param notGrantedPermissions
             */
            @Override
            public void onAuthorityNotAllGranted(ArrayList notGrantedPermissions) {
                Log.d("TAG","onAuthorityAllGranted");

            }
        };
        requestAllPermission(permissions,callback);

    }

    //配置ActionBar的菜单-OptionMenu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home_activity_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.home_activity_info:{
                Intent intent = new Intent(this, JsAboutActivity.class);
                intent.putExtra(JsAboutActivity.ImageResouceKey,R.mipmap.vgemv);
                intent.putExtra(JsAboutActivity.TitleKey,"骏视信息科技有限公司");
                String appVersionName = AppApplication.sharedInstance().getVersionName();
                String sdkVersionName = JsUtilitySdkManager.version;
                String env = Env.envName;
                String detail = String.format("JsUtilitySdkDemo(%s)\nJsUtilitySdk(%s)\nEnvironment:(%s)",appVersionName,sdkVersionName,env);
                intent.putExtra(JsAboutActivity.DetailKey,detail);
                startActivity(intent);
                break;
            }
        }
        return super.onOptionsItemSelected(item);
    }
}
